Zepto(function($){
    /*
     综合计算
     */
    /*
     yearPeriad:总期数(月)[商业贷款]
     money:本金[商业贷款 单位元]
     monthInterest:月利息[商业贷款 格式:x.xxxxxx 小数点后六位]
     money1:本金[公积金 单位元]
     monthInterest1:月利息[公积金 格式x.xxxxxx 小数点后六位]
     typeInterest:还息类型[1等额本金/0等额本息]
     typeCal:计算类型[0商业贷款/1公积金贷款/2组合贷款]
     */
    function CalLoan(yearPeriad, money, monthInterest, money1, monthInterest1, typeInterest, typeCal) {

        var HouseLoanObject = {};
        if (typeInterest == 1) {
            switch (typeCal) {
                case 0:
                    HouseLoanObject = CalAverageCapitalComm(yearPeriad, money, monthInterest);
                    break;
                case 1:
                    HouseLoanObject = CalAverageCapitalComm(yearPeriad, money1, monthInterest1);
                    break;
                case 2:
                    var HouseLoanObject1 = CalAverageCapitalComm(yearPeriad, money, monthInterest);
                    var HouseLoanObject2 = CalAverageCapitalComm(yearPeriad, money1, monthInterest1);
                    HouseLoanObject.BusInterest = HouseLoanObject1.Result;
                    HouseLoanObject.AccuInterest = HouseLoanObject2.Result;
                    HouseLoanObject.Result = HouseLoanObject1.Result + HouseLoanObject2.Result;
                    HouseLoanObject.ResultCount = HouseLoanObject1.ResultCount + HouseLoanObject2.ResultCount;
                    HouseLoanObject.MaxPayment = HouseLoanObject1.MaxPayment + HouseLoanObject2.MaxPayment;
                    HouseLoanObject.MaxInterest = HouseLoanObject1.MaxInterest + HouseLoanObject2.MaxInterest;

                    var houseLoan = {};
                    var houseLoanArray = new Array();

                    for (var i = 1; i <= yearPeriad; i++) {
                        houseLoan =
                        {
                            ReplyPrincipalIntreest: (HouseLoanObject1.HouseLoan[i - 1].ReplyPrincipalIntreest + HouseLoanObject2.HouseLoan[i - 1].ReplyPrincipalIntreest),
                            ReplyInterest: (HouseLoanObject1.HouseLoan[i - 1].ReplyInterest + HouseLoanObject2.HouseLoan[i - 1].ReplyInterest),
                            ReplyPrincipal: (HouseLoanObject1.HouseLoan[i - 1].ReplyPrincipal + HouseLoanObject2.HouseLoan[i - 1].ReplyPrincipal),
                            SurplusPrincipal: (HouseLoanObject1.HouseLoan[i - 1].SurplusPrincipal + HouseLoanObject2.HouseLoan[i - 1].SurplusPrincipal)
                        };
                        houseLoanArray.push(houseLoan);
                    }
                    HouseLoanObject.HouseLoan = houseLoanArray;
                    break;
            }
        }
        else {
            switch (typeCal) {
                case 0:
                    HouseLoanObject = CalAverageCapitalPlusInterestComm(yearPeriad, money, monthInterest);
                    break;
                case 1:
                    HouseLoanObject = CalAverageCapitalPlusInterestComm(yearPeriad, money1, monthInterest1);
                    break;
                case 2:
                    var HouseLoanObject1 = CalAverageCapitalPlusInterestComm(yearPeriad, money, monthInterest);
                    var HouseLoanObject2 = CalAverageCapitalPlusInterestComm(yearPeriad, money1, monthInterest1);
                    HouseLoanObject.BusInterest = HouseLoanObject1.Result;
                    HouseLoanObject.AccuInterest = HouseLoanObject2.Result;
                    HouseLoanObject.Result = HouseLoanObject1.Result + HouseLoanObject2.Result;
                    HouseLoanObject.ResultCount = HouseLoanObject1.ResultCount + HouseLoanObject2.ResultCount;
                    HouseLoanObject.MonthPayment = HouseLoanObject1.MonthPayment + HouseLoanObject2.MonthPayment;
                    HouseLoanObject.MaxPayment = HouseLoanObject1.MaxPayment + HouseLoanObject2.MaxPayment;
                    HouseLoanObject.MaxInterest = HouseLoanObject1.MaxInterest + HouseLoanObject2.MaxInterest;

                    var houseLoan = {};
                    var houseLoanArray = new Array();

                    for (var i = 1; i <= yearPeriad; i++) {
                        houseLoan =
                        {
                            ReplyPrincipalIntreest: (HouseLoanObject1.HouseLoan[i - 1].ReplyPrincipalIntreest + HouseLoanObject2.HouseLoan[i - 1].ReplyPrincipalIntreest),
                            ReplyInterest: (HouseLoanObject1.HouseLoan[i - 1].ReplyInterest + HouseLoanObject2.HouseLoan[i - 1].ReplyInterest),
                            ReplyPrincipal: (HouseLoanObject1.HouseLoan[i - 1].ReplyPrincipal + HouseLoanObject2.HouseLoan[i - 1].ReplyPrincipal),
                            SurplusPrincipal: (HouseLoanObject1.HouseLoan[i - 1].SurplusPrincipal + HouseLoanObject2.HouseLoan[i - 1].SurplusPrincipal)
                        };
                        houseLoanArray.push(houseLoan);
                    }

                    HouseLoanObject.HouseLoan = houseLoanArray;
                    break;
            }
        }
        /*
         BusInterest:组合计算商业贷款利息总额
         AccuInterest：组合计算公积金贷款利息总额
         Result:利息总额
         ResultCount:累计还款总额
         MonthPayment:每月月供
         MaxPayment:最高月供
         MaxInterest:最高利息
         HouseLoan:累计偿还利息集合
         */
        return HouseLoanObject;
    }
    /*计算公式=（总期数(月)+1）* 本金 *(月利率)/2
     月利率=年利率/12
     */
    /*
     通用等额本金计算规则
     利息总额=(总期数+1)*本金*月利率/2
     累计还款总额=本金+利息总额
     最高月供=（本金/总期数）+（本金-0）*月利息
     最高月付利息=本金*月利息^1
     */
    /*
     yearPeriad:总期数(月)
     money:本金[元]
     monthInterest:月利率
     */
    function CalAverageCapitalComm(yearPeriad, money, monthInterest) {
        /*商贷利息总额=（总期数(月)+1）* 本金 *(月利率)/2 */
        var result = (yearPeriad + 1) * money * monthInterest / 2;
        result = Math.abs(result.toFixed(2));

        /*累计还款总额=利息+本金*/
        var resultCount = parseFloat(result.toFixed(2)) + parseFloat(money);
        resultCount = Math.abs(resultCount);

        /*最高月供=(本金/总期数(月))+(本金-0)*月利息*/
        var maxPayment = (money / yearPeriad) + (money - 0) * monthInterest;
        maxPayment = Math.abs(maxPayment.toFixed(2));

        /*最高利息=本金*(年利率/12)^1*/
        var maxInterest = money * Math.pow(monthInterest, 1);
        maxInterest = Math.abs(maxInterest.toFixed(2));

        var houseLoan = {};
        var houseLoanArray = new Array();
        for (var i = 1; i <= yearPeriad; i++) {
            var replyPrincipalIntreest = (i != 1 ? ((money - (money / yearPeriad) * (i - 1)) * monthInterest) + (money / yearPeriad) : maxPayment); //偿还本息
            var replyInterest = (i != 1 ? ((money - (money / yearPeriad) * (i - 1)) * monthInterest) : maxInterest); //偿还利息
            var replyPrincipal = money / yearPeriad; //偿还本金
            var surplusPrincipal = money - (money / yearPeriad) * i; //剩余本金
            houseLoan = { ReplyPrincipalIntreest: replyPrincipalIntreest, ReplyInterest: replyInterest, ReplyPrincipal: replyPrincipal, SurplusPrincipal: surplusPrincipal };
            houseLoanArray.push(houseLoan);
        }
        var HouseLoanObject = {};
        HouseLoanObject.Result = result;
        HouseLoanObject.ResultCount = resultCount;
        HouseLoanObject.MaxPayment = maxPayment;
        HouseLoanObject.MaxInterest = maxInterest;
        HouseLoanObject.HouseLoan = houseLoanArray;
        return HouseLoanObject;
    }

    /*
     商业贷款等额本息计算通用方法
     偿还本息 = （本金*月利息）*(1+月利息)^贷款期限)/((1+月利息)^贷款期限-1）
     利息总额 = 偿还本息*总期数-本金
     累计还款总额 = 偿还本息 * 总期数
     每月月供=偿还本息
     最高月付利息=本金*月利息^1
     */
    /*
     yearPeriad:总期数(月)
     money:本金
     monthInterest:月利率
     */
    function CalAverageCapitalPlusInterestComm(yearPeriad, money, monthInterest) {
        /*偿还本息=(（本金*月利息）*(1+月利息)^贷款期限)/((1+月利息)^贷款期限-1）*/
        var repleyInterest = (money * monthInterest * Math.pow(1 + monthInterest, yearPeriad)) / (Math.pow(1 + monthInterest, yearPeriad) - 1);

        /*利息总额=偿还本息*总期数 - 本金*/
        var result = repleyInterest * yearPeriad - money;
        result = Math.abs(result.toFixed(2));

        /*累计还款总额=偿还本息* 总期数*/
        var resultCount = repleyInterest * yearPeriad;
        resultCount = Math.abs(resultCount.toFixed(2));

        /*每月月供=偿还本息*/
        var monthPayment = Math.abs(repleyInterest.toFixed(2));


        /*最高付款利息= 本金 * 月息^期次*/
        var maxPayment = money * Math.pow(monthInterest, 1);
        maxPayment = Math.abs(maxPayment.toFixed(2));
        var houseLoan = {};
        var houseLoanArray = new Array();
        for (var i = 1; i <= yearPeriad; i++) {
            var surplusPrincipal1 = money * Math.pow(1 + monthInterest, i) - repleyInterest * (Math.pow(1 + monthInterest, i) - 1) / monthInterest; /*剩余本金*/
            var surplusPrincipal2 = money * Math.pow(1 + monthInterest, i - 1) - repleyInterest * (Math.pow(1 + monthInterest, i - 1) - 1) / monthInterest; /*取上一次的本金计算每月偿还利息*/
            var repeyInt = surplusPrincipal2 * monthInterest; /*每月偿还利息*/
            var repeyPrincipal1 = Math.abs((repleyInterest - repeyInt).toFixed(2)); /*每月偿还本金*/

            var replyPrincipalIntreest = monthPayment; //偿还本息
            var replyInterest = (i != 1 ? repeyInt : maxPayment); //偿还利息
            var replyPrincipal = (i != 1 ? repeyPrincipal1 : (repleyInterest - maxPayment)); //偿还本金
            var surplusPrincipal = (i != 1 ? surplusPrincipal1 : (money - repleyInterest + maxPayment)); //剩余本金
            houseLoan = { ReplyPrincipalIntreest: replyPrincipalIntreest, ReplyInterest: replyInterest, ReplyPrincipal: replyPrincipal, SurplusPrincipal: surplusPrincipal };
            houseLoanArray.push(houseLoan);
        }
        var HouseLoanObject = {};
        HouseLoanObject.Result = result;
        HouseLoanObject.ResultCount = resultCount;
        HouseLoanObject.MonthPayment = monthPayment;
        HouseLoanObject.MaxPayment = maxPayment;
        HouseLoanObject.HouseLoan = houseLoanArray;
        return HouseLoanObject;
    }

        var selTypeCal = 0;
        var HouseLoanObject ;

        var txtTotalMoney = $('#totalMoney');
        var selSfRate     = $('#sfRate');
        var txtdkTotalMoney = $('#dkTotalMoney');
        var selajYear = $("#ajYear");
        var txtinitrate = $("#initrate");

        //组合贷款
        var txtGjMoney = $('#gjjMoney');
        var txtDkMoney = $('#dkMoney');
        var selAjyeardk = $('#ajyeardk');
        var txtInitrate1 = $('#initrate1');
        var txtInitrate2 = $('#initrate2');

        var m_sfMoney,m_dkMoney,m_typeCal = 0;

        //输入联动
        var sfRateFunciton = function (){
            var selIndex = selSfRate.get(0).selectedIndex;
            selSfRate.empty();
            for (var i=0;i<10;i++){
                var t_dkMoney = Math.round(txtTotalMoney.val() - (Math.round(txtTotalMoney.val() * i*10 ) / 100));
                var t_sfMoney = Math.round(txtTotalMoney.val()-t_dkMoney);
                if(selIndex == i){
                    selSfRate.append("<option  selected value='"+(i*10)+"'>"+(i*10)+"%"+"( " + t_sfMoney +" 万 )"+"</option>");
                    txtdkTotalMoney.attr("value", t_dkMoney);
                    m_dkMoney = t_dkMoney;
                    m_sfMoney = t_sfMoney;
                }else{
                    selSfRate.append("<option value='"+(i*10)+"'>"+(i*10)+"%"+"( " + t_sfMoney +" 万 )"+"</option>");
                }
            }
        }

        sfRateFunciton();

        $('#housePicTab li').click(function(){
            selTypeCal =   $(this).index();

            $('#housePicTab li').removeClass('on');
            $(this).addClass('on');

            $("#houseCal").hide();

            if(selTypeCal == 0){
                txtinitrate.val(4.90);
                $("#houseCalInput").show();
                $("#houseZHCalInput").hide();
                $("#detailPage").hide();
            }else if(selTypeCal == 1){
                txtinitrate.val(3.25);
                $("#houseCalInput").show();
                $("#houseZHCalInput").hide();
                $("#detailPage").hide();
            }else if(selTypeCal == 2){
                $("#houseCalInput").hide();
                $("#houseZHCalInput").show();
                $("#detailPage").hide();
            }
        });

        $('#payMethod a').click(function(){
            m_typeCal =   $(this).index();
            $('#payMethod a').removeClass('active');
            $(this).addClass('active');
            $("#detailPage").hide();
            calFun();
        });

        $("#detailLink").on("click",function(){
            $("#detailPage").show();
            location.href = "#detailPage";
            //if(selTypeCal!=2){
            $("#lIAllMoney").html(Math.round(HouseLoanObject.ResultCount)+"元");
            $("#lIDkMoney").html(Math.round(HouseLoanObject.ResultCount-HouseLoanObject.Result)+"元");
            $("#lILxMoney").html(Math.round(HouseLoanObject.Result)+"元");
            $("#lIDkYear").html("("+Math.round(HouseLoanObject.HouseLoan.length)+"月)("+Math.round(HouseLoanObject.HouseLoan.length)/12+"年)");
            $("#lIHkMoney").html(Math.round(HouseLoanObject.ResultCount/HouseLoanObject.HouseLoan.length)+"元/月");

            var htmlArr = [];
            htmlArr.push('<table border="0" cellpadding="0" cellspacing="0"><tbody><tr><th>月份</th><th>月供</th><th>月供本金</th><th>月供利息</th><th>剩余贷款</th></tr></tbody></table>');

            for(var i = 0;i<HouseLoanObject.HouseLoan.length/12 ;i++){
                htmlArr.push('<table style="width:100% ;border:0 ;cellpadding:0;cellspacing:0"><tbody><tr><td colspan="5" class="lf4">第'+(i+1)+'年</td></tr>');
                for(var j=0;j<12;j++){
                    htmlArr.push('<tr><td>'+(j+1)+'月</td><td>￥'+Math.round(HouseLoanObject.HouseLoan[(i*12+j)].ReplyPrincipalIntreest)+'</td><td>￥'+Math.round(HouseLoanObject.HouseLoan[(i*12+j)].ReplyPrincipal)+'</td><td>￥'+Math.round(HouseLoanObject.HouseLoan[(i*12+j)].ReplyInterest)+'</td><td>￥'+Math.round(HouseLoanObject.HouseLoan[(i*12+j)].SurplusPrincipal)+'</td></tr>')
                }
                htmlArr.push('</tbody></table>');
            }



            $("#listHtml").html(htmlArr.join(""));
            //}else{

           // }
        })

        $('#totalMoney').on("input",function(){
            if(txtTotalMoney.val() != ""){
                sfRateFunciton();
            }
        });

        $('#sfRate').on("change",function(){
            sfRateFunciton();
        });

        $("#btnCal").on("click",function(){
            $("#houseCal").show();
            $("#danduCal").show();
            $("#zhuheCal").hide();
            calFun();
        });

        $("#zhCalBtn").on("click",function(){
            $("#houseCal").show();
            $("#danduCal").hide();
            $("#zhuheCal").show();
            calFun();
        })


        var calFun = function(){

            location.href = "#houseCal";

            if(selTypeCal != 2){
                //贷款钱
                var dke = txtdkTotalMoney.val() *10000;
                //年份
                var ns = selajYear.val();
                //利率
                var nll = txtinitrate.val();

                if(selTypeCal == 0){
                    HouseLoanObject = CalLoan(ns*12,dke,nll/12/100,0,0,m_typeCal,selTypeCal);
                }else if(selTypeCal == 1){
                    HouseLoanObject = CalLoan(ns*12,0,0,dke,nll/12/100,m_typeCal,selTypeCal);
                }

                var doughnutData = [
                    {
                        value: m_sfMoney.toFixed(1),
                        color:"#6dbdff",
                        highlight: "#3bbdff"
                    },
                    {
                        value: Math.round(HouseLoanObject.Result/10000),
                        color: "#ff6ea0",
                        highlight: "#ff3ca0"
                    },
                    {
                        value: m_dkMoney.toFixed(1),
                        color: "#ffd87b",
                        highlight: "#ffd83f"
                    }

                ];

                var ctx = document.getElementById("pieConChar").getContext("2d");
                window.myDoughnut = new Chart(ctx).Doughnut(doughnutData, {responsive : true,scaleShowLabels: false});


                $("#monthPay").html("￥"+HouseLoanObject.HouseLoan[0].ReplyPrincipalIntreest.toFixed(1));
                $("#ddZj").html(Math.round(txtTotalMoney.val())+"万元");
                $("#ddSf").html(Math.round(m_sfMoney)+"万元");
                $("#ddDk").html(Math.round(m_dkMoney)+"万元");
                $("#ddLx").html(Math.round(HouseLoanObject.Result)+"元");
                $("#ddLr").html(txtinitrate.val()+"%");
                $("#ddYg").html(Math.round(HouseLoanObject.HouseLoan[0].ReplyPrincipalIntreest) +"元/月");

            }else{

                HouseLoanObject = CalLoan(selAjyeardk.val()*12,txtDkMoney.val()*10000,txtInitrate1.val()/12/100,txtGjMoney.val()*10000,txtInitrate2.val()/12/100,m_typeCal,selTypeCal);

                var doughnutData = [
                    {
                        value: Math.round(HouseLoanObject.ResultCount-HouseLoanObject.Result),
                        color:"#6dbdff",
                        highlight: "#3bbdff"
                    },
                    {
                        value: Math.round(HouseLoanObject.Result),
                        color: "#ffd87b",
                        highlight: "#ffd83f"
                    }

                ];

                var ctx = document.getElementById("pieConChar").getContext("2d");
                window.myDoughnut = new Chart(ctx).Doughnut(doughnutData, {responsive : true,scaleShowLabels: false});

                $("#monthPay").html("￥"+Math.round(HouseLoanObject.HouseLoan[0].ReplyPrincipalIntreest));

                $("#HkZj").html(Math.round(HouseLoanObject.ResultCount)+"元");
                $("#DkZj").html(Math.round(HouseLoanObject.ResultCount-HouseLoanObject.Result)+"元");
                $("#LxZj").html(Math.round(HouseLoanObject.Result)+"元");
                $("#ddYg2").html(Math.round(HouseLoanObject.HouseLoan[0].ReplyPrincipalIntreest)+"元/月");




            }




        }

});